//index.js
//获取应用实例
var app = getApp()
var baseUrl = 'https://manage.1miaodao.cn/wx';
Page({
  onLoad: function () {
  },

  getSuperToken: function () {
    wx.request({
      url: baseUrl + '/token/app',
      data: {
        ac: 'warcraft',
        se: '777'
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res.data);
        wx.setStorageSync('super_token', res.data.token);
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  
  getToken: function () {

    

    //调用登录接口
    
    wx.login({
      success: function (res) {
        var code = res.code;
        console.log('code');
        console.log(code);

          wx.request({
            url: baseUrl + '/token/user',
            data: {
              code: code
            },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            success: function (res) {
              console.log(res.data);
              wx.setStorageSync('token', res.data.data.token);
            },
            fail: function (res) {
              console.log(res.data);
            }
          })
        
      },
      fail:function(res){
        console.log(res)
      }
    })
  },

  checkSession: function () {
    wx.checkSession({
      success: function () {
        console.log('session success');
      },
      fail: function () {
        console.log('session fail');
      }
    })
  },

  delivery: function () {
    wx.request({
      url: baseUrl + '/order/delivery',
      header: {
        token: wx.getStorageSync('super_token')
      },
      method: 'PUT',
      data: {
        // id:wx.getStorageSync('order_id')
        id: 293
      },
      success: function (res) {
        console.log(res.data);
      }
    })
  },


  //未必在点击支付前，先点击一下申请令牌，确保令牌申请成功
  pay: function () {
    var token = wx.getStorageSync('token');
    var that = this;
    // that.getPreOrder(token, 'A303256065493535')
    wx.request({
      url: baseUrl + '/order',
      header: {
        token: token
      },
      data: {
        products:
        [
          {
            productId: '5ae295345ea10UiHi6dd4g', count: 1
          },
          {
            productId: '5ad97def2c308sOozRHnz1', count: 1, specid:'5ad97def47888lqUK8UTwh'
          }
          
        ],
        orderType:0,
        storeId:'5aefca8b04ee9KauxQu6nC',
        deliverAddress:'湖北省武汉市洪山区光谷广场',
        userName: '长江',
        userPhone: '13845212036',
        subscribeDate: '2018-05-07 18:16'
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.data.pass) {
          wx.setStorageSync('order_id', res.data.data.orderId);
          that.getPreOrder(token, res.data.data.orderId);
        }
        else {
          console.log('订单未创建成功');
        }
      }
    })
  },

  PayBlan:function(){
    var token = wx.getStorageSync('token');
    var that = this;

    wx.request({
      url: baseUrl + '/order',
      header: {
        token: token
      },
      data: {
        products:
        [
          {
            productId: '5ae295345ea10UiHi6dd4g', count: 1
          },
          {
            productId: '5ad97def2c308sOozRHnz1', count: 1, specid: '5ad97def47888lqUK8UTwh'
          }

        ],
        orderType: 0,
        storeId: '5aefca8b04ee9KauxQu6nC',
        deliverAddress: '湖北省武汉市洪山区光谷广场',
        userName: '长江',
        userPhone: '13845212036',
        subscribeDate: '2018-05-07 18:16'
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.data.pass) {
          wx.setStorageSync('order_id', res.data.data.orderId);
          that.getPreOrderBlan(token, res.data.data.orderId);
        }
        else {
          console.log('订单未创建成功');
        }
      }
    })
  },

  OpenGroup:function(){
    var token = wx.getStorageSync('token');
    var that = this;

    wx.request({
      url: baseUrl + '/MakeGroup',
      header: {
        token: token
      },
      data: {
        products:
        [
          {
            productId: '5ad9462c3e738qAeS-dSws', count: 1, specid:'5ad98b73ef808PQXHduEjd'
          }

        ],
        id:'609a40c1348g74de86ehpd1018c27029',
        grouptype:1,
        orderType: 0,
        deliverAddress: '湖北省武汉市洪山区光谷广场',
        userName: '长江',
        userPhone: '13845212036',
        subscribeDate: '2018-05-07 18:16'
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.data.pass) {
          wx.setStorageSync('order_id', res.data.data.orderId);
          that.getPreOrderBlan(token, res.data.data.orderId);
        }
        else {
          console.log('订单未创建成功');
        }
      }
    })
  },

  Group:function(){
    wx.navigateTo({
      url: '/pages/group/grouplist',
    })
    
  },

  myGroups:function(){
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: baseUrl + '/myGroups',
      header: {
        token: token
      },
      data: {
        
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        
      }
    })
  },

  myGroupDetails:function(){
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: baseUrl + '/myGroupDetails',
      header: {
        token: token
      },
      data: {
        unigroupId:'81feea745b94d29f4064e2f24d58697a'
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);

      }
    })
  },

  getPreOrderBlan: function (token, orderID){
    if (token) {
      wx.request({
        url: baseUrl + '/pay/balance',
        method: 'POST',
        header: {
          token: token
        },
        data: {
          id: orderID
        },
        success: function (res) {
          var preData = res.data.data;
          console.log('preData');
          console.log(res);
        
        }
      })
    }
  },
  getPreOrder: function (token, orderID) {
    if (token) {
      wx.request({
        url: baseUrl + '/pay/pre_order',
        method: 'POST',
        header: {
          token: token
        },
        data: {
          id: orderID
        },
        success: function (res) {
          var preData = res.data.data;
          console.log('preData');
          console.log(res);
          wx.requestPayment({
            timeStamp: preData.timeStamp.toString(),
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              console.log(res.data);
            },
            fail: function (error) {
              console.log(error);
            }
          })
        }
      })
    }
  },

  //佣金提现
  MakeMoeny:function(){
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: baseUrl + '/Sale/PutforwardApply',
      header:{
        token: token
      },
      data:{
        amount:10
      },
      method:'POST',
      success: function(res){
        console.log(res.data);
        if(res.data.status == 1){
          wx.setStorageSync('tx_order_id', res.data.data.id);
          that.getComOrder(token, res.data.data.id);
        }else{
          console.log('提现金额超过余额')
        }
      }
    })

  },
  getComOrder: function (token,orderId){
    if(token){
      console.log('66666');
      console.log(orderId);
      wx.request({
        url: baseUrl + '/Sale/MakeMoney',
        method:'POST',
        header: {
          token: token
        },
        data:{
          id: orderId
        },
        success: function(res){
          console.log(res)
        }
      })
    }
  },

  Mytest:function(){
    var token = wx.getStorageSync('token');
    var that = this;
    wx.request({
      url: baseUrl + '/index',
      header: {
        token: token
      },
      data: {

      },
      method: 'POST',
      success: function (res) {
        console.log(res.data);

      }
    })
  },

  formID: function (event) {
    console.log(event);
  },
  /**
  * 上传身份证正面照
  */
  UploadImg: function (e) {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
    
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: baseUrl + '/uploadimg',
          filePath: tempFilePaths[0],
          name: 'images',
          success: function (res) {
            if (res.errMsg == "uploadFile:ok") {
              // if (that.data.codeIdImg) {
              //   network._post_json(that, api.delOneImg, {
              //     imgUrl: that.data.codeIdImg.imgUrl
              //   }, function (that, res) { });
              // }
              // that.setData({
              //   codeIdImg: JSON.parse(res.data).data
              // })
              console.log(JSON.parse(res.data).data)
            }
          }
        })
      }
    })
  }
})
